﻿using mediaControl.Event;
using mediaControl.Utils;
using mediaControl.Vo;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace mediaControl.ViewModels
{
    public class UpdateProgramViewModel : BindableBase, IDialogAware
    {
        public string Title{get;set;}

        public event Action<IDialogResult> RequestClose;

        private String programName = "";

        private ProgramVo programVo;

        private ProgramItemVo programItemVo;

        //事件聚合器
        private readonly IEventAggregator eventAggregator;

        public UpdateProgramViewModel(IEventAggregator eventAggregator)
        {
            ConfirmCommand = new DelegateCommand(confirm);
            this.eventAggregator = eventAggregator;
        }

        private void confirm()
        {
            if (String.IsNullOrEmpty(this.ProgramName))
            {
                MessageBox.Show("名称不能为空！");
                return;
            }
            String fileFullPath = ProgramVo.FilePath + "\\" + ProgramVo.Name + ".info"; // 指定要创建的文件路径及名称
            
            try
            {
                if (!File.Exists(fileFullPath))
                {
                    MessageBox.Show("节目不存在！");
                    return;
                }
                string content = WriteFileUtil.readFile(fileFullPath);
                JObject programJson = JsonConvert.DeserializeObject<JObject>(content);
                ObservableCollection<ProgramItemVo> tempList = JsonConvert.DeserializeObject<ObservableCollection<ProgramItemVo>>((String)programJson.GetValue("programItems"));
                foreach (ProgramItemVo programItemVo in tempList)
                {
                    if (programItemVo.No==ProgramItemVo.No)
                    {
                        programItemVo.Name = ProgramName;
                    }
                }
                programJson["programItems"] = JsonConvert.SerializeObject(tempList);
                ProgramItemVo.Name = ProgramName;
                WriteFileUtil.writeFile(fileFullPath, JsonConvert.SerializeObject(programJson), "2");
               
                //通知列表显示
                eventAggregator.GetEvent<ProgramItemEvent>().Publish(ProgramItemVo);
                IDialogParameters parameters = new DialogParameters();
                parameters.Add("programItem", ProgramItemVo);
                RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK, parameters));

            }
            catch (Exception ex)
            {
                MessageBox.Show($"无法创建文件：{ex.Message}");
                return;
            }
        }

        /// <summary>
        /// 修改文件，保存文件信息
        /// </summary>
        public DelegateCommand ConfirmCommand { set; get; }

        public String ProgramName
        {
            get { return programName; }
            set { programName = value; RaisePropertyChanged(); }
        }

        public ProgramVo ProgramVo { get => programVo; set => programVo = value; }
        public ProgramItemVo ProgramItemVo { get => programItemVo; set => programItemVo = value; }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            programItemVo = parameters.GetValue<ProgramItemVo>("programItem");
            ProgramVo = parameters.GetValue<ProgramVo>("program");
            ProgramName = programItemVo.Name;
            this.Title = "节目重命名";
        }
    }
}
